﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;

namespace br.com.SistemaChamados.QueryManager.ReaderParser
{
    public class DataBinder
    {

        private const string INTEGER_PARSE = "System.Int32";
        private const string STRING_PARSE = "System.String";
        private const string DOUBLE_PARSE = "System.Double";
        private const string DECIMAL_PARSE = "System.Decimal";
        private const string BOOLEAN_PARSE = "System.Boolean";
        private const string DATETIME_PARSE = "System.DateTime";

        public static object bindData(IDataReader aReader, int fieldPosition, Type fieldType)
        {
            object dataToBind = aReader[fieldPosition];
            //Executing convert data
            string describeTypeField = fieldType.FullName;
            switch (describeTypeField)
            {
                case INTEGER_PARSE:
                    return Convert.ToInt32(dataToBind);
                case STRING_PARSE:
                    return dataToBind.ToString();
                case DOUBLE_PARSE:
                    return Convert.ToDouble(dataToBind);
                case DECIMAL_PARSE:
                    return Convert.ToDecimal(dataToBind);
                case BOOLEAN_PARSE:
                    bool result = Convert.ToBoolean(dataToBind);
                    return result;
                case DATETIME_PARSE:
                    return Convert.ToDateTime(dataToBind);
                default:
                    throw new Exception("Unsupported type parser! ["+fieldType.FullName+"]");
            }
        }

    }
}